<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
// 引入饼状图组件
require('echarts/lib/chart/pie')
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

const animationDuration = 3000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '400px'
    },
    dataJson: Array
  },
  data() {
    return {
      chart: null,
    }
  },
  watch: {
    dataJson(newval, oldval) {
      this.dataJson = newval
      this.initChart()
    }
  },
  mounted() {
    console.log(this.dataJson)
    setTimeout(()=>{
      this.initChart()
    }, 500)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption({
        title: {
          text: '订单概况统计',
          subtext: '订单概况统计',
          left: 'center'
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          orient: 'vertical',
          left: '10%',
          data: ['未付款的订单','待确认的订单','待发货的订单','已发货的订单','已完成的订单','取消的订单']
        },
        series: [{
          name: '订单概况统计',
          type: 'pie',
          radius: '55%',
          roam: false,
          center: ['50%', '60%'],
          emphasis: {
						itemStyle: {
							shadowBlur: 10,
							shadowOffsetX: 0,
							shadowColor: 'rgba(0, 0, 0, 0.5)'
						}
					},
          data: this.dataJson,
          animationDuration: animationDuration
        }]
      }, true)
    }
  }
}
</script>
